package com.ym.response;

import lombok.Getter;

import java.util.function.Supplier;

@Getter
public class Result<T> {
    // Getter 方法
    private int code;
    private String message;
    private T data;

    private Result() {}

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功结果的静态方法
    public static <T> Result<T> ok(T data) {
        return new Result<>(200, "success", data);
    }

    // 成功结果的静态方法（无数据）
    public static <T> Result<T> ok() {
        return new Result<>(200, "success", null);
    }

    // 失败结果的静态方法
    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message, null);
    }

    // 失败结果的静态方法（默认错误码）
    public static <T> Result<T> fail(Supplier<T> data) {
        return new Result<>(500, "fail", data.get());
    }
}
